/**
 * 
 */
package com.alstudio.ui.module.notice;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.formater.sex.ALSexFormatter;
import com.alstudio.utils.formater.time.ALTimeUtils;
import com.alstudio.view.absadapter.ViewAdapter;
import com.loovee.imaohu.R;

/**
 * 留言信箱adapter
 * 
 * @author alonso lee
 * 
 */
public class UserNoticeAdapter extends ViewAdapter {

	private ArrayList<ALMessage> data = new ArrayList<ALMessage>();

	// 是否启用最右边的imageview
	private boolean isAddonIconUse = false;

	private int addonIconRes = -1;

	private boolean showDelete = false;

	private int layoutRes = -1;

	private boolean isNickOnly = false;

	public interface AddOnIconClickListener {
		public void onAddOnIconClick(Object obj);
	}

	private AddOnIconClickListener addOnIconClickListener;
	private AddOnIconClickListener deleteIconClickListener;

	private Context context;

	/**
	 * 
	 */
	public UserNoticeAdapter(Context context, ArrayList<ALMessage> data) {
		// TODO Auto-generated constructor stub
		this.data = data;
		this.context = context;
	}

	/**
	 * @param layoutRes
	 *            the layoutRes to set
	 */
	public void setLayoutRes(int layoutRes) {
		this.layoutRes = layoutRes;
	}

	private class ViewHolder {
		public TextView nick_name, time, location_age, sig, delet;
		public ImageView image, sex, vauth, addon_icon;
		public View time_layout;
	}

	public void setNickOnly(boolean isNickOnly) {
		this.isNickOnly = isNickOnly;
	}

	public boolean isNickOnly() {
		return isNickOnly;
	}

	/**
	 * @param isAddonIconUse
	 *            the isAddonIconUse to set
	 */
	public void setAddonIconUse(boolean isAddonIconUse) {
		this.isAddonIconUse = isAddonIconUse;
	}

	/**
	 * @return the isAddonIconUse
	 */
	public boolean isAddonIconUse() {
		return isAddonIconUse;
	}

	/**
	 * @param addonIconRes
	 *            the addonIconRes to set
	 */
	public void setAddonIconRes(int addonIconRes) {
		this.addonIconRes = addonIconRes;
	}

	/**
	 * @return the addonIconRes
	 */
	public int getAddonIconRes() {
		return addonIconRes;
	}

	/**
	 * @param showDelete
	 *            the showDelete to set
	 */
	public void setShowDelete(boolean showDelete) {
		this.showDelete = showDelete;
	}

	/**
	 * @return the showDelete
	 */
	public boolean isShowDelete() {
		return showDelete;
	}

	/**
	 * @param addOnIconClickListener
	 *            the addOnIconClickListener to set
	 */
	public void setAddOnIconClickListener(
			AddOnIconClickListener addOnIconClickListener) {
		this.addOnIconClickListener = addOnIconClickListener;
	}

	/**
	 * @return the addOnIconClickListener
	 */
	public AddOnIconClickListener getAddOnIconClickListener() {
		return addOnIconClickListener;
	}

	/**
	 * @param deleteIconClickListener
	 *            the deleteIconClickListener to set
	 */
	public void setDeleteIconClickListener(
			AddOnIconClickListener deleteIconClickListener) {
		this.deleteIconClickListener = deleteIconClickListener;
	}

	/**
	 * @return the deleteIconClickListener
	 */
	public AddOnIconClickListener getDeleteIconClickListener() {
		return deleteIconClickListener;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final ViewHolder holder;
		if (convertView == null) {
			int reslayout = R.layout.user_notice_item_layout;
			if (layoutRes != -1) {
				reslayout = layoutRes;
			}
			view = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
					.inflate(reslayout, parent, false);

			holder = new ViewHolder();
			holder.time = (TextView) view.findViewById(R.id.time);
			holder.nick_name = (TextView) view.findViewById(R.id.nick_name);
			holder.location_age = (TextView) view
					.findViewById(R.id.location_age);
			holder.sex = (ImageView) view.findViewById(R.id.sex);
			holder.vauth = (ImageView) view.findViewById(R.id.vauth);

			holder.image = (ImageView) view.findViewById(R.id.icon);
			holder.sig = (TextView) view.findViewById(R.id.sig);
			holder.addon_icon = (ImageView) view.findViewById(R.id.addon_icon);

			holder.delet = (TextView) view.findViewById(R.id.delete_bt);

			holder.time_layout = view.findViewById(R.id.time_layout);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		setupViewItem(holder, position);
		return view;
	}

	private void setupViewItem(ViewHolder holder, int pos) {
		ALMessage u = data.get(pos);
		holder.nick_name.setText(u.getFromNick());

		ALLocalEnv.getLocalEnv().displayImagePluginServerlet(u.getFromAvatar(),
				holder.image,
				ALLocalEnv.getLocalEnv().getRoundImageDisplayOption());

		if (isNickOnly) {
			holder.location_age.setVisibility(View.GONE);
			holder.sig.setVisibility(View.GONE);
			holder.time_layout.setVisibility(View.GONE);
			holder.delet.setVisibility(View.GONE);
			holder.sex.setVisibility(View.GONE);
			holder.vauth.setVisibility(View.GONE);
			holder.addon_icon.setVisibility(View.GONE);
			return;
		}

		// 留言时间
		holder.location_age.setText(ALTimeUtils.formartVoipStartTime(context, u
				.getDate().getTime()));

		holder.sex.setImageResource(ALSexFormatter.transSexBigDrawable(u
				.getSex()));

		if ((ALNumericUtils.parseInt(u.getVauth(), 0) == 1)) {
			holder.vauth.setImageResource(R.drawable.find_user_icon_vip_big);
		} else {
			holder.vauth.setImageBitmap(null);
		}

		if (showDelete) {
			holder.delet.setVisibility(View.VISIBLE);
			holder.addon_icon.setVisibility(View.GONE);
			setupDeleteIconClickEvent(holder.delet, pos);
		} else {
			holder.delet.setVisibility(View.GONE);
			holder.addon_icon.setVisibility(View.VISIBLE);
			holder.delet.setOnClickListener(null);
		}

		int imgRes = 0;
		holder.addon_icon.setBackgroundResource(imgRes);

		if (u.getDisplayed()) {
			if (u.isPlaying()) {
				imgRes = R.drawable.leave_audio_stop_green_btn_selector;
			} else {
				imgRes = R.drawable.leave_audio_play_gray_btn_selector;
			}
		} else {
			if (u.isPlaying()) {
				imgRes = R.drawable.leave_audio_stop_green_btn_selector;
			} else {
				imgRes = R.drawable.leave_audio_play_green_btn_selector;
			}
		}

		holder.addon_icon.setBackgroundResource(imgRes);

		// 右边图标
		setupAddOnIconClickEvent(holder.addon_icon, u);

	}

	private void setupAddOnIconClickEvent(View view, Object data) {
		view.setTag(data);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (addOnIconClickListener != null) {
					addOnIconClickListener.onAddOnIconClick(v.getTag());
				}
			}
		});
	}

	private void setupDeleteIconClickEvent(View view, int pos) {
		view.setTag(pos);

		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (deleteIconClickListener != null) {
					deleteIconClickListener.onAddOnIconClick(v.getTag());
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.absadapter.ViewAdapter#getRealCount()
	 */
	@Override
	public int getRealCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

}
